package com.example.text;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class VolumeHuan extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volume_huan);
        Button button=findViewById(R.id.return4);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Button button1=findViewById(R.id.action);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText=findViewById(R.id.inputvolume);
                String str=editText.getText().toString().trim();
                if(str.isEmpty())
                {
                    editText.setText("别空着");
                }
                else
                {
                    boolean judge=true;
                    for(int i=0;i<str.length();i++)
                    {
                        char num=str.charAt(i);
                        if(num!='0'&&num!='1'&&num!='2'&&num!='3'&&num!='4'&&num!='5'&&num!='6'&&num!='7'&&num!='8'
                                &&num!='9'&&num!='.')
                        {
                            editText.setText("请输入数字");
                            judge=false;
                            break;
                        }
                    }
                    if(judge)
                    {
                        Double num=Double.valueOf(str);

                        TextView textView=findViewById(R.id.Volume_dm);
                        String str1=format(num*0.001);
                        textView.setText(str1+"(dm^3)");

                        TextView textView2=findViewById(R.id.Volume_m);
                        String str2=format(num*0.000001);
                        textView2.setText(str2+"(m^3)");

                        TextView textView3=findViewById(R.id.Volume_km);
                        String str3=format(num*0.000000000000001);
                        textView3.setText(str3+"(km^3)");

                        TextView textView4=findViewById(R.id.Volume_mm);
                        String str4=format(num*1000);
                        textView4.setText(str4+"(mm^3)");

                        TextView textView5=findViewById(R.id.Volume_oz);
                        String str5=format(num*0.0338);
                        textView5.setText(str5+"(usoz)");

                        TextView textView6=findViewById(R.id.Volume_gal);
                        String str6=format(num*0.000264);
                        textView6.setText(str6+"(usgal)");

                    }
                    else{}
                }
            }
        });

    }
    public String format(double value){
        BigDecimal bd=new BigDecimal(value);
        bd=bd.setScale(6, RoundingMode.CEILING.HALF_UP);
        return bd.toString();
    }

}